package dujiu.leetcode.greed;

import java.util.Arrays;

public class P2279 {
    public int maximumBags(int[] capacity, int[] rocks, int additionalRocks) {
        int capacityNum = 0;
        int[] result = new int[capacity.length];
        for (int i = 0; i < capacity.length; i++) {
            result[i] = capacity[i] - rocks[i];
        }
        Arrays.sort(result);
        for (int i = 0; i < result.length; i++) {
            if (additionalRocks <= 0) {
                break; // 石头用完，提前退出
            }
            if (result[i] == 0){
                capacityNum++;
                continue;
            }
            if (additionalRocks >= result[i]){
                additionalRocks -= result[i];
                capacityNum++;
            }
        }
        return capacityNum;
    }

    public static void main(String[] args) {
        P2279 p = new P2279();
        int[] capacity = {2,3,4,5};
        int[] rocks = {1,2,4,4};
        System.out.println(p.maximumBags(capacity, rocks, 2));
    }
}
